#!/usr/bin/perl

use strict;

use Config::Simple;
use DBI;
use File::Basename;
use POSIX qw(strftime);

my $CFG_FILE = '@CFGDIR@/codepot.ini';
my $REPO = $ARGV[0];
my $REPOBASE = basename($REPO);
my $REV = $ARGV[1];
my $USER = $ARGV[2];
my $PROPNAME = $ARGV[3];
my $ACTION = $ARGV[4];

my $QC = '';

# [STDIN] PROPVAL  ** the old property value is passed via STDIN.

sub get_config
{
	my $cfg = new Config::Simple();
	
	if (!$cfg->read ($CFG_FILE))
	{
		return undef;
	}
	
	my $config = {
		database_hostname => $cfg->param ("database_hostname"),
		database_port => $cfg->param ("database_port"),
		database_username => $cfg->param ("database_username"),
		database_password => $cfg->param ("database_password"),
		database_name => $cfg->param ("database_name"),
		database_driver => $cfg->param ("database_driver"),
		database_prefix => $cfg->param ("database_prefix")
	};

	return $config;
}

sub open_database
{
	my ($cfg) = @_;

	my $dbtype = $cfg->{database_driver};
	my $dbname = $cfg->{database_name};
	my $dbhost = $cfg->{database_hostname};
	my $dbport = $cfg->{database_port};

	if ($dbtype eq 'postgre') { $dbtype = 'Pg'; }
	elsif ($dbtype eq 'oci8') { $dbtype = 'Oracle'; }
	elsif ($dbtype eq 'mysqli') { $dbtype = 'mysql'; }

	my $dbstr;
	my $dbuser;
	my $dbpass;
	if ($dbtype eq 'Oracle')
	{
		$QC = '"';
		$dbstr = "DBI:$dbtype:";
		$dbuser = $cfg->{database_username} . '/' . $cfg->{database_password} . '@' . $dbhost;
		$dbpass = '';
	}
	else
	{
		$dbstr = "DBI:$dbtype:database=$dbname;";
		if (length($dbhost) > 0) { $dbstr .= "host=$dbhost;"; }
		if (length($dbport) > 0) { $dbstr .= "port=$dbport;"; }

		$dbuser = $cfg->{database_username};
		$dbpass = $cfg->{database_password};
	}

	my $dbh = DBI->connect(
		$dbstr, $dbuser, $dbpass,
		{ RaiseError => 0, PrintError => 0, AutoCommit => 0 }
	);

	return $dbh;
}

sub close_database
{
	my ($dbh) = @_;
	$dbh->disconnect ();
}

sub write_revprop_change_log
{
	my ($dbh, $prefix, $projectid, $revision, $userid, $propname, $action) = @_;

#+------+---------+-----------+---------------------------+---------------------+---------------+-----------------+
#| id   | type    | projectid | message                   | createdon           | action        | userid          |
#+------+---------+-----------+---------------------------+---------------------+---------------+-----------------+
#| 1610 | code    | codepot   | svn,codepot,98,svn:log,M  | 2014-05-16 22:27:36 | revpropchange | hyunghwan.chung | 

	my $message = "svn,$projectid,$revision,$propname,$action";
	my $createdon = strftime ('%Y-%m-%d %H:%M:%S', localtime());

	$dbh->begin_work ();

	my $query = $dbh->prepare ("INSERT INTO ${QC}${prefix}log${QC} (${QC}type${QC},${QC}projectid${QC},${QC}message${QC},${QC}createdon${QC},${QC}action${QC},${QC}userid${QC}) VALUES (?,?,?,?,?,?)");
	if (!$query || !$query->execute ('code', $projectid, $message, $createdon, 'revpropchange', $userid))
	{
		my $errstr = $dbh->errstr();
		$query->finish ();
		$dbh->rollback ();
		return (-1, $errstr);
	}

	$query->finish ();
	$dbh->commit ();
	return (0, undef);
}

#------------------------------------------------------------
# MAIN
#------------------------------------------------------------

my $cfg = get_config ();
if (!defined($cfg))
{
	print (STDERR "Cannot load codepot configuration file\n");
	exit (1);
}

my $dbh = open_database ($cfg);
if (!defined($dbh))
{
	printf (STDERR "Cannot open database - %s\n", $DBI::errstr);
	exit (1);
}

write_revprop_change_log ($dbh, $cfg->{database_prefix}, $REPOBASE, $REV, $USER, $PROPNAME, $ACTION);
close_database ($dbh);

exit (0);

